package com.rraj.common.redis;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.rraj.common.redis.dataRedis.RedisClient;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * Created by WangSheng on 2017/7/5.
 *
 * @author WangSheng
 * @date 2017/07/05
 */
public class BaseRedisKey{
    /**
     * format的格式
     */
    private String keyFormat;
    /**
     * 单个操作的格式化的值
     */
    private String[] keyValues;
    /**
     * 批量操作的格式化的值
     */
    private Collection<String[]> batchKeyValues;
    /**
     * 过期时间段
     */
    private Long expireSecond;
    /**
     * 过期时间段单位
     */
    private TimeUnit expireSecondUnit = TimeUnit.SECONDS;
    /**
     * 过期时间点
     */
    private Date expireDate;
    /**
     * 单个操作的原生的key
     */
    private String originKey;
    /**
     * 批量操作的原生的key
     */
    private Collection<String> batchOriginKey;

    public String getKeyFormat() {
        return keyFormat;
    }

    public void setKeyFormat(String keyFormat) {
        this.keyFormat = keyFormat;
    }

    public String[] getKeyValues() {
        return keyValues;
    }

    public void setKeyValues(String[] keyValues) {
        this.keyValues = keyValues;
    }

    public Collection<String[]> getBatchKeyValues() {
        return batchKeyValues;
    }

    public void setBatchKeyValues(Collection<String[]> batchKeyValues) {
        this.batchKeyValues = batchKeyValues;
    }

    public Long getExpireSecond() {
        return expireSecond;
    }

    public void setExpireSecond(Long expireSecond) {
        this.expireSecond = expireSecond;
    }

    public TimeUnit getExpireSecondUnit() {
        return expireSecondUnit;
    }

    public void setExpireSecondUnit(TimeUnit expireSecondUnit) {
        this.expireSecondUnit = expireSecondUnit;
    }

    public Date getExpireDate() {
        return expireDate;
    }

    public void setExpireDate(Date expireDate) {
        this.expireDate = expireDate;
    }

    public String getOriginKey() {
        return originKey;
    }

    public void setOriginKey(String originKey) {
        this.originKey = originKey;
    }

    public Collection<String> getBatchOriginKey() {
        return batchOriginKey;
    }

    public void setBatchOriginKey(Collection<String> batchOriginKey) {
        this.batchOriginKey = batchOriginKey;
    }



    /**
     * 获取存入数据库的key
     */
    public String getDataBaseKey(){
        if(isFormatKey()){
            return getFormatKey();
        }
        return originKey;
    }
    /**
     * 判断key是否需要format
     * @return
     */
    public boolean isFormatKey(){
        return StrUtil.isNotBlank(keyFormat);
    }
    /**
     * 获取格式化后的key
     * @return
     */
    public String getFormatKey(){
        return RedisClient.format(keyFormat, keyValues);
    }
    /**
     * 判断是否是batch操作
     * @return
     */
    public boolean isBatchKey(){
        return CollectionUtil.isNotEmpty(batchOriginKey) || CollectionUtil.isNotEmpty(batchKeyValues);
    }
    /**
     * 获取批量操作的key的set集合
     * @return
     */
    public Collection<String> getBatchKeyCollection(){
        if(isFormatKey()){
            Set<String> keySet = new HashSet<>();
            for(String[] strs : batchKeyValues){
                String key = RedisClient.format(keyFormat, strs);
                keySet.add(key);
            }
            return keySet;
        }
        return batchOriginKey;
    }

    /**
     * 判断是否是按时间点设置过期时间
     * @return
     */
    public boolean isExpireAt(){
        return ObjectUtil.isNotNull(expireDate);
    }
    /**
     * 判断是否是按时间段设置过期时间
     * @return
     */
    public boolean isExpirePeriod(){
        return ObjectUtil.isNotNull(expireSecond);
    }
    /**
     * 根据时间点获取过期时间段
     * @return 单位是毫秒，如果小于当前时间，则设置过期时间为-1
     */
    public Long getExpirePeriod(){
        if(isExpirePeriod()){
            return expireSecond * 1000;
        }else if(isExpireAt()){
            long milliSecond =  expireDate.getTime() - DateUtil.current(false);
            if(milliSecond < 0){
                return -1L;
            }
            return milliSecond;
        }
        return -1L;
    }
}
